

#ifndef __CCE_MATERIAL_MATERIALVIEW_H__
#define __CCE_MATERIAL_MATERIALVIEW_H__

#include <qgraphicsview.h>
#include <qgraphicsitem.h>
#include <stdio.h>


#define CCE_MATERIAL_ITEM_HBORDER  2
#define CCE_MATERIAL_ITEM_VBORDER  2
#define CCE_MATERIAL_BUTTON_SIZE   12
#define CCE_MATERIAL_ITEM_TEXTURE_SIZE 128

/**
 * \brief Base class for button like items.
 */
class cceMaterialButtonItem : public QGraphicsObject
{
	Q_OBJECT
protected:
	QGraphicsRectItem*	_background;
	QBrush	_brushNormal;
	QBrush	_brushHover;

	cceMaterialButtonItem (QGraphicsItem* parent = 0);
protected:
	void hoverEnterEvent (QGraphicsSceneHoverEvent *evt);
	void hoverLeaveEvent (QGraphicsSceneHoverEvent *evt);
	void mousePressEvent (QGraphicsSceneMouseEvent *evt);
	void mouseReleaseEvent (QGraphicsSceneMouseEvent *evt);

	void SetBrushes (const QBrush &normal, const QBrush &hover);

public:
	virtual QRectF boundingRect() const;
	virtual void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);

signals:
	void clicked ();
};

/**
 * \brief Button item with a 'plus' visual
 */
class cceMaterialAddItem : public cceMaterialButtonItem
{
private:
	QGraphicsLineItem*	_line0;
	QGraphicsLineItem*	_line1;
public:
	cceMaterialAddItem (QGraphicsItem* parent = 0);
};

/**
 * \brief Button item with a 'minus' visual
 */
class cceMaterialRemoveItem : public cceMaterialButtonItem
{
private:
	QGraphicsLineItem*	_line;
public:
	cceMaterialRemoveItem (QGraphicsItem* parent = 0);
};

/**
 * \brief Button item with a 'arrow up' visual
 */
class cceMaterialMoveUpItem : public cceMaterialButtonItem
{
private:
	QGraphicsPathItem*	_path;
public:
	cceMaterialMoveUpItem (QGraphicsItem* parent = 0);
};

/**
 * \brief Button item with a 'arrow down' visual
 */
class cceMaterialMoveDownItem : public cceMaterialButtonItem
{
private:
	QGraphicsPathItem*	_path;
public:
	cceMaterialMoveDownItem (QGraphicsItem* parent = 0);
};

class cceMaterialView;
class cceMaterialBaseItem : public QGraphicsObject
{
	Q_OBJECT
protected:
  unsigned _minWidth;
  unsigned _minHeight;

  unsigned _width;
  unsigned _height;

  QPen  _textPen;
  QFont _font;

  bool    _clickable;

  bool    _unifiedSize;

	cceMaterialView	*_view;

  QList<cceMaterialBaseItem*> _children;
protected:
  cceMaterialBaseItem (QGraphicsItem* parent = 0);
  QList<cceMaterialBaseItem*>& GetChildrenList ();

public:
  virtual void ComputeSize () = 0;
  virtual void UpdateLayout () = 0;

  bool Contains (cceMaterialBaseItem* item);
	void AddChild (cceMaterialBaseItem* item);
  void AddChild (cceMaterialBaseItem *item, int idx);
	void RemoveChild (cceMaterialBaseItem* item);

  const QList<cceMaterialBaseItem*>& GetChildren () const;

  void SetClickable (bool clickable);
  bool IsClickable () const;

  void SetUnifiedSize (bool unifiedSize);
  bool IsUnifiedSize () const;

  unsigned GetMinWidth () const;
  unsigned GetMinHeight () const;

  void SetWidth (unsigned width);
  void SetHeight (unsigned height);

  unsigned GetWidth () const;
  unsigned GetHeight () const;

  QPen GetTextPen () const;
  QFont GetFont () const;

	void SetView (cceMaterialView* view);
	cceMaterialView* GetView () const;

  void Debug (int ind)
  {
    for (unsigned i=0; i<ind; ++i)
      {
        printf ("  ");
      }
    printf ("View: '0x%p'\n", _view);
    fflush(stdout);
    foreach (cceMaterialBaseItem* item, _children)
      {
        item->Debug(ind+1);
      }
  }

  virtual QRectF boundingRect() const;
  virtual void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
};

class cceMaterialValueItem : public cceMaterialBaseItem
{
	Q_OBJECT
private:
  QGraphicsRectItem         *_background;

  QColor                    _backgroundColor, _backgroundColorHover;

  QGraphicsSimpleTextItem   *_typeItem;
  QGraphicsSimpleTextItem   *_nameItem;
  QGraphicsLineItem         *_separatorItem;

  cceMaterialButtonItem     *_removeItem;

protected slots:
  virtual void RemoveValue () { }

  void hoverEnterEvent (QGraphicsSceneHoverEvent *evt);
  void hoverLeaveEvent (QGraphicsSceneHoverEvent *evt);
  void mousePressEvent(QGraphicsSceneMouseEvent *event);
  void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);

public:
  cceMaterialValueItem (QGraphicsItem* parent = 0);

  void SetType (const QString& type);
  void SetName (const QString& name);

  void SetBackgroundColor (const QColor& color);

  virtual void ComputeSize ();
  virtual void UpdateLayout ();

signals:
  void Clicked ();

};

class cceMaterialSimpleValueItem : public cceMaterialBaseItem
{
  Q_OBJECT
protected:
  QGraphicsRectItem*        _background;
  QGraphicsSimpleTextItem*	_nameItem;

protected slots:
  void hoverEnterEvent (QGraphicsSceneHoverEvent *evt);
  void hoverLeaveEvent (QGraphicsSceneHoverEvent *evt);

public:
  cceMaterialSimpleValueItem (QGraphicsItem* parent = 0);

  void SetName (const QString& name);
  void SetBackgroundColor (const QColor& color);
  void SetTextColor (const QColor& color);

  virtual void ComputeSize ();
  virtual void UpdateLayout ();

};


class cceMaterialSlotItem : public cceMaterialBaseItem
{
private:
  QGraphicsRectItem*        _background;
  QGraphicsSimpleTextItem*	_nameItem;
public:
  cceMaterialSlotItem (QGraphicsItem* parent = 0);

  void SetName (const QString& name);


  virtual void ComputeSize ();
  virtual void UpdateLayout ();
};


class cceMaterialTechniqueItem : public cceMaterialBaseItem
{
	Q_OBJECT
private:
  QGraphicsRectItem*	_background;

  QGraphicsSimpleTextItem*	_nameItem;

  cceMaterialMoveUpItem*    _upItem;
  cceMaterialMoveDownItem*  _downItem;
  cceMaterialRemoveItem*    _removeItem;

protected slots:
	virtual void RemoveTechnique () { } 
	virtual void MoveUp ()  { }
	virtual void MoveDown () { } 



public:
  cceMaterialTechniqueItem (bool upItem = true, bool downItem = true, bool removeItem = true);

  void SetName (const QString& name);

  virtual void ComputeSize ();
  virtual void UpdateLayout ();
};


class cceMaterialPrimaryItem : public cceMaterialBaseItem
{
	Q_OBJECT
private:
	QGraphicsRectItem*	_background;

	QGraphicsSimpleTextItem*	_typeItem;
	QGraphicsSimpleTextItem*	_nameItem;
	QGraphicsLineItem*	_separatorItem;

	cceMaterialButtonItem*	_addItem;

protected slots:
  virtual void AddTechnique () { }


public:
  cceMaterialPrimaryItem (QGraphicsItem* parent = 0, bool addItem = true);

	void SetType (const QString& type);
	void SetName (const QString& name);

  virtual void ComputeSize ();
  virtual void UpdateLayout ();
};



class cceMaterialView : public QGraphicsView
{
private:

  cceMaterialBaseItem* _primaryItem;

public:
	cceMaterialView (QWidget* parent = 0);

  void SetItem (cceMaterialBaseItem* item);

  void UpdateLayout ();


};


#endif /* ! __CCE_MATERIAL_MATERIALVIEW_H__ */

